package com.gogowan.agroenterprise.newview;

import java.util.List;

import com.gogowan.agroenterprise.R;
import com.gogowan.agroenterprise.base.BaseActivity;
import com.gogowan.agroenterprise.contorl.MyAsyncTask;
import com.gogowan.agroenterprise.db.DBHelper;
import com.gogowan.agroenterprise.model.GetbaseInfoRequest;
import com.gogowan.agroenterprise.model.GetbaseInfoResult;
import com.gogowan.agroenterprise.model.LoginResultInfo;
import com.gogowan.agroenterprise.model.MaterialBase;
import com.gogowan.agroenterprise.utils.Utils;

import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ImageButton;
import android.widget.Toast;

public class NewCollectSelect extends BaseActivity {

	// 登录信息
	
	private DBHelper dbHelper;
	private ProgressDialog progressDialog;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_collect_select);
		findViewById(R.id.new_collect).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(NewCollectSelect.this, NewCollectScan.class);
				startActivity(intent);
			}
		});
		findViewById(R.id.history_collect).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				startActivity(new Intent(NewCollectSelect.this, NewCollectHistory.class));
			}
		});
		findViewById(R.id.data_sync).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				initDataSync();

			}
		});
		ImageButton fhBtn = (ImageButton) findViewById(R.id.in_storage_fh_btn);
		fhBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});
		dbHelper = new DBHelper(this);
		dbHelper.openDatabase();
		List<MaterialBase> list = dbHelper.getBaseData();
		if (list == null || list.size() == 0) {
			initError("未查到基础产品信息，请重新登录后重试");
		}
	}

	private void initDataSync() {
		SharedPreferences preferences = getSharedPreferences("LoginINfo", MODE_PRIVATE);
		
		
		GetbaseInfoRequest request = new GetbaseInfoRequest();
		//request.setEnterpriseId(loginResultInfo.getEnterpriseinfo().getEnterpriseid());
		DataSyncTask dataSyncTask = new DataSyncTask();
		progressDialog = ProgressDialog.show(this, getString(R.string.alert_title), getString(R.string.alert_message),
				true, false);
		dataSyncTask.execute(request);
	}

	class DataSyncTask extends MyAsyncTask {

		@Override
		protected Object doInBackground(Object... params) {
			GetbaseInfoRequest getbaseInfoRequest = (GetbaseInfoRequest) params[0];
			String json = Utils.toJSON(getbaseInfoRequest);
			com.gogowan.agroenterprise.model.Message message = new com.gogowan.agroenterprise.model.Message(31000, true,
					json);
			String res = Utils.postCommit(getString(R.string.first_url) + getString(R.string.first_url_page), message);
			return res;
		}

		@Override
		protected void onPostExecute(Object res) {
			if (isFinishing()) {
				return;
			}
			progressDialog.cancel();
			try {
				if (res == null || res.equals("")) {
					initError(getString(R.string.server_error));
				} else {
					com.gogowan.agroenterprise.model.Message message = (com.gogowan.agroenterprise.model.Message) Utils
							.fromJSON((String) res, com.gogowan.agroenterprise.model.Message.class);
					String string = Utils.getMessageContent(message);

					if (string == null || string.equals("")) {
						initError(getString(R.string.analyse_error));
					} else {
						GetbaseInfoResult getbaseInfoResult = (GetbaseInfoResult) Utils.fromJSON(string,
								GetbaseInfoResult.class);
						if (getbaseInfoResult.getResult() == 31000) {
							if (getbaseInfoResult.getMaterialInfo() != null
									&& getbaseInfoResult.getMaterialInfo().size() > 0) {
								dbHelper.initDataDictinary(getbaseInfoResult.getMaterialInfo());
								initError(getbaseInfoResult.getResultinfo());
							}
						} else {
							initError(getbaseInfoResult.getResultinfo());
						}
					}
				}
			} catch (Exception ex) {
				initError(getString(R.string.analyse_error));
			}
			super.onPostExecute(res);
		}
	}

	private void initError(String msg) {
		Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
	}
}
